<template>
  <div class="app-container">
                <el-form   ref="queryRef" :inline="true"  v-show="showSearch">
                    <el-form-item label="获奖等级" prop="leader" label-width="70px"> 
                         <el-select
                        placeholder="请选择获奖等级"
                        clearable
                            style="width: 240px"
                            v-model="queryParams.awardLevel"
                            size="small"

                    >
                    <el-option
                        v-for="item in awardLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
                    </el-form-item>
                    <el-form-item label="获奖类型" prop="category" label-width="115px">
                       <el-select
                        placeholder="请选择获奖等级"
                        clearable
                            style="width: 240px"
                            v-model="queryParams.awardType"
                            size="small"

                    >
                    <el-option
                        v-for="item in awardType"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>

                    </el-form-item>
                     <el-form-item label="字典名称" prop="category" label-width="115px">
                       <el-input
                            v-model="queryParams.awardName"
                            placeholder="请输入字典名称"
                            clearable
                            size="small"
                            style="width: 240px"
                       />
                     </el-form-item>
                    <el-form-item>
                    <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
                    <el-button icon="Tickets" size="mini" @click="handleRef">参考</el-button>
                    </el-form-item>
                </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
       <el-col :span="1.5">
        <el-button
        type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
           @click="handleUpdate"
        >修改</el-button>
       </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="代码"  align="center" prop="code"  width="80"/>
      <el-table-column
        label="字典名称"
        align="center"
        prop="awardName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="奖励类型" align="center" width="90" prop="awardType" />
      <el-table-column label="获奖水平" align="center" width="90" prop="awardLevel" />
      <el-table-column
        label="获奖等级"
        align="center"
        prop="awardGrade"
        width="90"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="工作量Max" align="center" prop="workloadMax"  width="90" />
      <el-table-column label="工作量Min" align="center" prop="workloadMin"  width="90" />
      <el-table-column label="备注" align="center" prop="remark" width="100" overflow:hidden  :show-overflow-tooltip="true"/>    
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="160" />       
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdTime"
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form ref="dictRef" :model="form" :rules="rules" label-width="100px">
           <el-form-item label="分类代码" prop="code">
               <el-input  placeholder="请输入分类代码" v-model="form.code"/>
          </el-form-item>
        <el-form-item label="获奖名称" prop="awardName">
          <el-input v-model="form.awardName"
                    placeholder="请输入获奖名称"/>
        </el-form-item>
        <el-form-item label="获奖类型" prop="awardType">
          <el-select
                        placeholder="请选择获奖类型"
                        clearable
                        style="width:  260px"
                        v-model="form.awardType"
                    >
                    <el-option
                        v-for="item in awardType"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
        </el-form-item>
        <el-form-item label="获奖等级" prop="awardGrade">
                    <el-select
                        placeholder="请选择获奖等级"
                        clearable
                        style="width:  260px"
                        v-model="form.awardGrade"
                    >
                    <el-option
                        v-for="item in awardGrade"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
        </el-form-item>
        <el-form-item label="获奖水平" prop="awardLevel">
                    <el-select
                        placeholder="请选择获奖水平"
                        clearable
                        style="width:  260px"
                        v-model="form.awardLevel"
                    >
                    <el-option
                        v-for="item in awardLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
        </el-form-item>
          <el-form-item label="工作量Max" prop="workloadMax">
               <el-input-number  :min="0"
                                 placeholder="请输入工作量最大值" 
                                 controls-position="right"
                                 v-model="form.workloadMax"
                                 style="width: 260px"/>
          </el-form-item>
          <el-form-item label="工作量Min" prop="workloadMin">
               <el-input-number  :min="0"
                                 placeholder="请输入工作量最小值" 
                                 controls-position="right"
                                 v-model="form.workloadMin"
                                 style="width: 260px"/>
          </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
                <!-- 参考表单 -->
<el-dialog :title="title" v-model="refOpen" width="650px" append-to-body>
    <el-table :data="refData" border style="width: 100%">
            <el-table-column prop="number" label ="序 号" width="80" align="center"/>
            <el-table-column prop="paper" label ="论文、成果转化"  align="center" />
            <el-table-column prop="category" label="类别" width="100" align="center" />
            <el-table-column prop="workload" label="工作量" width="100" align="center" />
        </el-table>
</el-dialog>
  </div>
</template>

<script setup name="Dict">
import { refreshCache } from "@/api/system/dict/type";
import {
  getDict,
  listDict,
  addDict,
  updateDict,
  delDict,
} from "@/api/nepu_dict/award";
import {ref} from 'vue';


const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const awardType = ref([
  {value: '科研获奖',label: '科研获奖'},
  {value: '教学获奖',label: '教学获奖'},
])
const awardGrade = ref([
  {value: '一等奖',label: '一等奖'},
  {value: '二等奖',label: '二等奖'},
  {value: '三等奖',label: '三等奖'},
])
    const refData = [
   {
      number:'1',
      paper:'横向课题到账经费500万元以上',
      category:'A',
      workload:'500'
   },
    {
      number:'2',
      paper:'横向课题到账经费200-500万元',
      category:'A',
      workload:'200-500'
   }
   ,
    {
      number:'3',
      paper:'横向课题到账经费100-200万（人文社会科学30万）元以上（含）',
      category:'A',
      workload:'100-200'
   },
   {
      number:'4',
      paper:'横向课题到账经费50-100万元（人文社会科学10-30万）',
      category:'B',
      workload:'50-100'
   },
   {
      number:'5',
      paper:'横向课题（人文社会科学）到账经费2-10万元',
      category:'B',
      workload:'10-50'
   },
]
const awardLevel = ref([
        {value: 'A1',label: 'A1'},
        {value: 'A2',label: 'A2'},
        {value: 'A3',label: 'A3'},
        {value: 'A4',label: 'A4'},
        {value: 'A5',label: 'A5'},
        {value: 'B1',label: 'B1'},
        {value: 'B2',label: 'B2'},
        {value: 'B3',label: 'B3'},
        {value: 'B4',label: 'B4'},
        {value: 'C1',label: 'C1'},
        {value: 'C2',label: 'C2'},
        ])
const typeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const refOpen = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    //  dictName: undefined,
    //  dictType: undefined,
    //  status: undefined,
    order: "DESC",
    sortBy: "id",
    awardLevel: undefined,
    awardName: undefined,
    awardType: undefined,
  },
   rules: {
    //v-modle.number才可以
    workloadMax: [
      { required: true, message: "不能为空", trigger: "blur" },
      { type: "number", min: 0,  message: "工作量不得小于0" },
    ],
    workloadMin: [
      { required: true, message: "不能为空", trigger: "blur" },
      { type: "number", min: 0,  message: "工作量不得小于0" },
    ],
     awardName:[
       {required: true, message: "不能为空", trigger: "blur"}
     ],
    awardType: [
      { required: true, message: "不能为空", trigger: "blur" },
      {
        min: 2,
        max: 50,
        message: "类型名称长度必须介于 2 和 50 之间",
        trigger: "change",
      },
    ],
    awardLevel: [
      { required: true, message: "不能为空", trigger: "change" },
    ],

    code:[
      {required: true, message: "不能为空", trigger: "blur" }
    ]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询字典类型列表 */
function getList() {
  loading.value = true;
  listDict(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      typeList.value = response.content;
      total.value = response.totalElements;
      loading.value = false;
    }
  );
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    awardGrade: undefined,
    awardLevel: undefined,
    awardName: undefined,
    awardType: undefined,
    code:undefined,
    workloadMax: 0,
    workloadMin:0
  };
  proxy.resetForm("dictRef");
}

  function resetQueryParams(){
        queryParams.value = {
            pageNum: 1,
            pageSize: 10,
            //  dictName: undefined,
            //  dictType: undefined,
            //  status: undefined,
            order: "DESC",
            sortBy: "id",
            awardLevel: undefined,
            awardName: undefined,
            awardType: undefined,
        }
    }
/** 搜索按钮操作 */
function handleQuery() {
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
        resetQueryParams()
        getList()
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加字典类型";
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const dictId = row.id || ids.value;
  getDict(dictId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改字典类型";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dictRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateDict(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDict(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const dictIds = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？')
    .then(function () {
      return delDict(dictIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/dict/type/export",
    {
      ...queryParams.value,
    },
    `dict_${new Date().getTime()}.xlsx`
  );
}
/** 刷新缓存按钮操作 */
function handleRefreshCache() {
  refreshCache().then(() => {
    proxy.$modal.msgSuccess("刷新成功");
  });
}
    /** 参考表单 */
function handleRef() {
    title.value = '参考'
    refOpen.value = true
}
getList();
</script>
